package com.sandasai.action.user;
import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sandasai.service.UserManager;
import com.sandasai.vo.UserBean;

@Controller
@Scope("prototype")
public class LoginAction extends ActionSupport{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource
	private UserManager userManager;
	
    private String usernameOrEmail;
    private String password;
    
    public String getUsernameOrEmail() {
        return usernameOrEmail;
    }
    public void setUsernameOrEmail(String usernameOrEmail) {
        this.usernameOrEmail = usernameOrEmail;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    /**
     * @return
     */
    public String execute() {
        UserBean userBean = userManager.getUserBean(usernameOrEmail, password);
        ActionContext ctx = ActionContext.getContext();
        if(userBean == null) {
            ctx.put("message", "登录失败！");			
            return ERROR;
        }else {
            ctx.getSession().put("username", userBean.getUsername());
            ctx.getSession().put("userType", userBean.getUserType());
            ctx.put("message", "登录成功！");
            if(userBean.getUserType() == UserBean.USER_TYPE_STUDENT) {
                return "student";
            }else if(userBean.getUserType() == UserBean.USER_TYPE_TEACHER) {
                return "teacher";
            }else if(userBean.getUserType() == UserBean.USER_TYPE_ADMIN){
                return "admin";
            }else {
                return ERROR;
            }
        }
    }
}